<?php

namespace App\Http\Controllers\Mobile\Company;
use App\Facades\Style;
use App\Http\Requests\Company\Basic\MobileBasicRequest;
use App\Models\Company\CompanyBasic;
use Illuminate\Http\Request;


class BasicController extends CommonController{
    /**
     * 基本资料填写
     * @param Request $request
     * @return mixed
     */
    public function info(Request $request){
        $userInfo = auth()->guard('company')->user();
        $CompanyBasic=CompanyBasic::where('uid',$userInfo->uid)->orderBy('updatetime', 'desc')->first();
        //企业不存在时
        if(empty($CompanyBasic)){
            return redirect("mobile/company/register_basic");
        }
        return Style::view('mobile.company.basicInfo',compact('CompanyBasic'));
    }

    /**
     * 基本资料提交
     * @param MobileBasicRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function postInfo(MobileBasicRequest $request){
        $input = $request->only('company_name', 'brands', 'comkind', 'industry', 'established', 'employee_num', 'temptation','profile',
            'contacts','mobile','phone','fax','email','qq','region','address','longitude','latitude','coordinate_address','website','lines');
        //获取用户信息ID
        $userInfo = auth()->guard('company')->user();
        $input['updatetime'] = date('Y-m-d H:i:s',time());

        if($request->input('id')){
            $companyBasic = CompanyBasic::where('uid',$userInfo->uid)->where('id',$request->input('id'))->count();
            if($companyBasic){
                CompanyBasic::where('uid',$userInfo->uid)->where('id',$request->input('id'))->update($input);
                return redirect('mobile/company/basic_info')->with('ok',"保存成功");
            }else{
                return Style::view('errors.404', ['exception' => '错误未知id...'], 404);
            }
        }else{
            $input['uid'] = $userInfo->uid;
            CompanyBasic::create($input);
            return redirect('mobile/company/welcome');
        }
    }

    /**
     * logo上传
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function logoUpload(Request $request){
        if ($request->hasFile('file')) {
            $file=$request->file('file');
            $entension = strtolower($file -> getClientOriginalExtension());
            //$mimeTye = $file -> getMimeType();
            $clientName = $file -> getClientOriginalName();//客服端文件名
            if ($file->isValid()) {
                if (!in_array($entension, array('jpg', 'gif', 'png'))){
                    return response()->json(['error' => 304, 'info' => "文件类型只能为jpg、gif、png！"]);
                }else{
                    $userInfo = auth()->guard('company')->user();
                    $company_basic=CompanyBasic::where('uid',$userInfo->uid)->orderBy('updatetime', 'desc')->first(['id', 'logo']);
                    if(!$company_basic){
                        return response()->json(['error' => 404, 'info' => "没有找到基本信息！"]);
                    }
                    if($company_basic->logo){
                        @unlink(public_path().'/upload/companylogo/'.$company_basic->logo);
                    }
                    $fileurl=date("Ym").'/';
                    $filename=time().strtolower(str_random(5)).'.'.$entension;
                    $request->file('file')->move(public_path().'/upload/companylogo/'.$fileurl,$filename);

                    $company_basic->logo = $fileurl.$filename;
                    $company_basic->save();

                    return response()->json(['status' => 200, 'url' => asset('upload/companylogo/'.$fileurl.$filename)]);
                }
            }
        }else{
            return response()->json(['error' => 306, 'info' => "没有选择上传图片文件,或者文件过大！"]);
        }
    }

}